/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package com.niannian.activity;

import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.bean.MyFamilyMemberBean;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.UmengPage;
import com.niannian.view.CircleImageView;
import com.niannian.web.nnSyncApi;
import com.niannian.BaseActivity;
import com.niannian.R;
import com.umeng.analytics.MobclickAgent;

/**
 * 家庭成员详情
 * 
 * @author shengl
 * 
 */
public class FamilyMemberDetailActivity extends BaseActivity {
	CircleImageView reg_family_detail_head_img;
	TextView tv_family_detail_nickname, tv_family_detail_phone, tv_family_detail_gender, tv_family_detail_birth, tv_no_family, tv_send_regard, tv_phone;

	public static final int EDIT_INFO = 1;
	MyFamilyMemberBean fdata = new MyFamilyMemberBean();
	int position = 0;
	private long mExitTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family_member_detail);
		initView();
		setLister();
		initTopView();
		Bundle b = getIntent().getExtras();
		fdata = (MyFamilyMemberBean) b.get("data");

		position = b.getInt("position", 0);
		initDta();
		new refreshData().init(null).execute();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("个人信息");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);
		iv_top_right.setImageResource(R.drawable.edit);

		// if (userInfoManager.family_perm == 1) {
		// iv_top_right.setVisibility(View.VISIBLE);
		// } else {
		// iv_top_right.setVisibility(View.GONE);
		// }
		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doback();
			}
		});

		iv_top_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle pBundle = new Bundle();
				pBundle.putSerializable("data", fdata);
				openActivity(FamilyMemberEditActivity.class, pBundle, EDIT_INFO);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
	}

	private void initView() {
		reg_family_detail_head_img = (CircleImageView) findViewById(R.id.reg_family_detail_head_img);
		tv_family_detail_nickname = (TextView) findViewById(R.id.tv_family_detail_nickname);
		tv_family_detail_phone = (TextView) findViewById(R.id.tv_family_detail_phone);
		tv_family_detail_gender = (TextView) findViewById(R.id.tv_family_detail_gender);
		tv_family_detail_birth = (TextView) findViewById(R.id.tv_family_detail_birth);

		tv_no_family = (TextView) findViewById(R.id.tv_no_family);

		tv_send_regard = (TextView) findViewById(R.id.tv_send_regard);
		tv_phone = (TextView) findViewById(R.id.tv_phone);

	}

	private void setLister() {

		reg_family_detail_head_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (System.currentTimeMillis() - mExitTime > INTERVAL) {
					Intent intent = new Intent(FamilyMemberDetailActivity.this, OriginalImage.class);
					intent.putExtra("display", Common.getAvatarThumbs(fdata.getAvatar()));
					intent.putExtra("OriginalImageUrl", Common.getAvatarOriginals(fdata.getAvatar()));
					FamilyMemberDetailActivity.this.startActivity(intent);
					mExitTime = System.currentTimeMillis();
				}
			}
		});
		tv_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + fdata.getPhone()));
				startActivity(intent);
				startAnimationLeftToRight();
				MobclickAgent.onEvent(FamilyMemberDetailActivity.this, UmengPage.MAKE_CALL);
			}
		});

		tv_send_regard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FamilyMemberDetailActivity.this, PushRegardsActivity.class);
				intent.putExtra("avatar", fdata.getAvatar());
				intent.putExtra("uid", fdata.getId());
				intent.putExtra("from", PushRegardsActivity.FROM_PUSH_REGARDS_CONFIRM);
				startActivity(intent);
				startAnimationLeftToRight();
				MobclickAgent.onEvent(FamilyMemberDetailActivity.this, UmengPage.profile_add_regard);
			}
		});
	}

	private void initDta() {

		myApplication.finalbitmap.display(reg_family_detail_head_img, Common.getAvatarThumbs(fdata.getAvatar()));
		tv_family_detail_nickname.setText(fdata.getNickname());
		tv_family_detail_phone.setText(fdata.getPhone() + "");

		if ("M".equals(fdata.getGender())) {
			tv_family_detail_gender.setText("男");
		} else if ("F".equals(fdata.getGender())) {
			tv_family_detail_gender.setText("女");
		} else {
			tv_family_detail_gender.setText("");
		}
		tv_family_detail_birth.setText(fdata.getBirth());
		// top_title.setText(fdata.getInnername());

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (RESULT_OK != resultCode) {
			return;
		}
		switch (requestCode) {
		case EDIT_INFO:
			MyFamilyMemberBean rdata = (MyFamilyMemberBean) data.getExtras().get("rdata");
			if (!Common.empty(rdata)) {
				fdata = rdata;
				initDta();
			}
			break;

		default:
			break;
		}
	}

	protected class refreshData extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			// Common.tip(FamilyMemberDetailActivity.this, "刷新数据失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			try {
				int retCode = paramJSONObject.getInt("code");
				if (retCode == ErrorCode.OK) {
					// Common.tip(FamilyMemberDetailActivity.this, "刷新数据成功");
					JSONObject fJSONObject = paramJSONObject.getJSONObject("data");
					fdata.setId(fJSONObject.getInt("id"));
					fdata.setNickname(fJSONObject.getString("nickname"));
					fdata.setAvatar(fJSONObject.getString("avatar"));
					fdata.setInnername(fJSONObject.getString("innername"));
					fdata.setPhone(fJSONObject.getString("phone"));
					fdata.setBirth(fJSONObject.getString("birth"));
					fdata.setGender(fJSONObject.getString("gender"));

					fdata.setFamily(fJSONObject.getInt("family"));

					if (userInfoManager.family_perm == 1 && userInfoManager.family == fdata.getFamily()) {
						iv_top_right.setVisibility(View.VISIBLE);
					} else {
						iv_top_right.setVisibility(View.GONE);
					}
					if (userInfoManager.family != fdata.getFamily()) {
						tv_no_family.setVisibility(View.VISIBLE);
					} else {
						tv_no_family.setVisibility(View.GONE);
					}

					initDta();
				} else if (retCode == ErrorCode.OBJECT_NOT_FOUND) {
					Common.tip(FamilyMemberDetailActivity.this, ErrorCode.GetErrorMsg(retCode));
				} else {
					Common.tip(FamilyMemberDetailActivity.this, paramJSONObject.getString("msg"));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.getFamilyMemberInfo(fdata.getId());
		}
	}

	protected void doback() {
		Intent intent = new Intent();
		intent.putExtra("rdata", fdata);
		intent.putExtra("position", position);
		setResult(RESULT_OK, intent);
		myfinish();
	}

	public void onBackPressed() {
		doback();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPage.PROFILE_VIEW);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPage.PROFILE_VIEW);
		MobclickAgent.onPause(this);
	}

}
